﻿var bTestMode=true;         // test mode
var iRefreshTimeout=250000;  //milliseconds img refresh rate (250 seconds)
var sMapURL;                // MAP URL
var tool="arrow"; // tool in use: "arrow" || "rotate" || "zoom"

var oPageTimeout=null;
var MyLasso = new lasso();

var xPos, yPos; // Map Position
var rotation; // Map Rotation
var size; //img size in pixel
var iMap; // selected map (integer)

var TitleBar=null;

function init()
{
    LoadSettings();
    
    System.Gadget.settingsUI = "settings.htm";
    System.Gadget.onSettingsClosed = init;


    var oBackground = document.getElementById("imgBackground");
    oBackground.src = "url(images/background.png)";                  
    
    if (TitleBar == null)
    {
        TitleBar = oBackground.addTextObject(".", "Calibri", 12, "White", 5, 5);    
    }
    TitleBar.width = 100;
    TitleBar.value = "PolipoMatico: " + maps[iMap].ShortName;
    
    if (bTestMode)
    {
        sMapURL = maps[iMap].URL;
        //sMapURL = "http://www.charlespetzold.com/PetzoldBW.gif";
    }
    
    SetToolIcon();
    
    refresh();
}

function refresh()
{
    LoadSettings();
    
    var oMap = document.getElementById("iMap");   
    oMap.src="";
   	oMap.style.top = yPos;
   	oMap.style.left = xPos;
   	oMap.style.width = oMap.style.height = size + "px";
   	oMap.rotation = rotation;

   	oMap.src = sMapURL + "?ts=" + formatCurrentDateTime();
   	
   	if (oPageTimeout!=null)
   	    clearTimeout( oPageTimeout );
   	    
	oPageTimeout = setTimeout("refresh()", iRefreshTimeout );
}

function formatCurrentDateTime() {
    var today = new Date();
    var TodayAsString = '';
    TodayAsString = TodayAsString.concat(normalizeNumber(today.getDate()),
                                                normalizeNumber(today.getMonth()),
                                                normalizeNumber(today.getFullYear()),
                                            normalizeNumber(today.getHours()),
                                            normalizeNumber(today.getMinutes()));
    return TodayAsString;
}

function normalizeNumber(number) {
    var NumberAsString = number.toString();

    if (NumberAsString.length == 1) {
        return '0' + NumberAsString;
    }
    if (NumberAsString.length == 4) {
        return NumberAsString.substr(2, 2);
    }
    else {
        return NumberAsString;
    }
}

function hMouseDown()
{
    if (tool=="arrow")
    {
        MyLasso.StartValue = new point (xPos,yPos);
        MyLasso.getMouseXY(MyLasso.StartPoint);
        MyLasso.on = true;
    }
    if (tool == "zoom") {
        MyLasso.getMouseXY(MyLasso.StartPoint);
        MyLasso.StartValue = size;
        MyLasso.on = true;
    }
    if (tool == "rotate")
    {
        MyLasso.getMouseXY(MyLasso.StartPoint);
        MyLasso.StartValue = rotation;
        MyLasso.on = true;
    }
   
    return false;
}

function hMouseMove()
{
    if (MyLasso.on)
    {
        if (tool=="arrow")
        {
            MyLasso.getMouseXY(MyLasso.CurrentPoint);
            
            xPos = parseInt( MyLasso.StartValue.X - MyLasso.dx());
            yPos = parseInt( MyLasso.StartValue.Y - MyLasso.dy());
            
            CheckBoundaries();
            
            document.getElementById("iMap").style.left = xPos;
            document.getElementById("iMap").style.top = yPos;
        }
        if (tool=="rotate")
        {
            MyLasso.getMouseXY(MyLasso.CurrentPoint);
            rotation = MyLasso.StartValue + Math.min(MyLasso.dx(), MyLasso.dy()) / 2;
            document.getElementById("iMap").rotation = rotation;
        }
        if (tool == "zoom") {
            MyLasso.getMouseXY(MyLasso.CurrentPoint);
            size = MyLasso.StartValue + Math.min(MyLasso.dx(), MyLasso.dy()) / 2;
            document.getElementById("iMap").style.width = document.getElementById("iMap").style.height = size + "px";
        }
        
        SaveSettings();
    }
    
    return false;
}

function hMouseUp()
{
    MyLasso.on = false;
    return false;
}

function hMouseOver()
{
    fade("idTool");
    return false;
}

function hMouseOut()
{
    MyLasso.on = false;
    fade("idTool");
    return false;
}

function hMouseDblClick() {
    xPos = yPos = 0;
    size = 100;
    rotation = 0;
    
    CheckBoundaries();
    SaveSettings();
    refresh();
}

function hMouseWheel()
{
    var newSize = 0;
    var oMap = document.getElementById("iMap");
    
    if (event.wheelDelta >= 0)
    {
        newSize = size + 20; 
	} 
	else if (event.wheelDelta <= 0) 
	{
        newSize = size - 20; 
	}
	
	if (size<50) size = 50;
	if (size>1000) size = 1000;
	
	xPos = xPos - (newSize - size)/2;
	yPos = yPos - (newSize - size)/2;
	
	CheckBoundaries();
	SaveSettings();
	
	oMap.style.width = oMap.style.height = newSize + "px";
	oMap.style.left = xPos;
	oMap.style.top  = yPos;
	
	size = newSize;
	
    return false;
}

function hClickTool()
{
    document.getElementById("idTool");

    if (tool == "arrow") {
        tool = "rotate";
        SetToolIcon();
        return;
    }

    if (tool == "rotate") {
        tool = "zoom";
        SetToolIcon();
        return;
    }

    if (tool == "zoom") {
        tool = "arrow";
        SetToolIcon();
        return;
    }

    return false;
}

function SetToolIcon()
{
    if (tool=="arrow") 
        document.getElementById("idTool").src="images/ArrowQuad32.png";
    if (tool=="zoom")
        document.getElementById("idTool").src = "images/ArrowUD.png";
    if (tool=="rotate")
        document.getElementById("idTool").src="images/Sync32.png";
}

function CheckBoundaries()
{
    var bodyX,bodyY;
    var obj = document.getElementById("gadgetContent");
      
    bodyX = obj.style.width.replace("px","");
    bodyY = obj.style.height.replace("px","");
    
    if (xPos>bodyX/2)
        xPos = bodyX/2;
    
    if (yPos>bodyY/2)
        yPos = bodyY/2;
    
    if ((size + xPos)<bodyX/2)
        xPos = - size + bodyX/2;
        
    if ((size + yPos)<bodyY/2)
        yPos = - size + bodyY/2;
}

function LoadSettings()
{
    xPos = System.Gadget.Settings.read("xPos");
    yPos = System.Gadget.Settings.read("yPos");
    size = System.Gadget.Settings.read("size");
    rotation = System.Gadget.Settings.read("rotation");
    iMap = System.Gadget.Settings.read("map");
    
    if (xPos == "") xPos = 0;
    if (yPos == "") yPos = 0;
    if (size == "") size = 100;
    if (rotation == "") rotation = 0.0;
    if (iMap == "") iMap = 0;
}

function SaveSettings()
{
    System.Gadget.Settings.write("xPos", xPos);
    System.Gadget.Settings.write("yPos", yPos);
    System.Gadget.Settings.write("size", size);
    System.Gadget.Settings.write("rotation", rotation);
    System.Gadget.Settings.write("map", iMap);
}